
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>odespy.rkf45 &mdash; Odespy API 0.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/pyramid.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="Odespy API 0.2 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Neuton&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Nobile:regular,italic,bold,bolditalic&amp;subset=latin" type="text/css" media="screen" charset="utf-8" />
<!--[if lte IE 6]>
<link rel="stylesheet" href="../../_static/ie6.css" type="text/css" media="screen" charset="utf-8" />
<![endif]-->

  </head>
  <body>

    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Odespy API 0.2 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for odespy.rkf45</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;Module for wrapping rkf45.py.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">solvers</span> <span class="kn">import</span> <span class="n">Solver</span><span class="p">,</span> <span class="n">Adaptive</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="RKF45"><a class="viewcode-back" href="../../rkf45.html#odespy.rkf45.RKF45">[docs]</a><span class="k">class</span> <span class="nc">RKF45</span><span class="p">(</span><span class="n">Adaptive</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Wrapper for rkf45.f, a FORTRAN solver designed to solve non-stiff and</span>
<span class="sd">    mildly stiff differential equations by the well-known</span>
<span class="sd">    Runge-Kutta-Fehlberg (4,5) method.</span>

<span class="sd">    The FORTRAN source rkf45.f can be obtained from Netlib.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">quick_description</span> <span class="o">=</span> <span class="s">&quot;Adaptive Runge-Kutta-Fehlberg (4,5) method (rkf45.f)&quot;</span>

    <span class="n">_optional_parameters</span> <span class="o">=</span> <span class="n">Adaptive</span><span class="o">.</span><span class="n">_optional_parameters</span> <span class="o">+</span> <span class="p">[</span><span class="s">&#39;f_f77&#39;</span><span class="p">]</span>
    <span class="c"># The following step parameters are illegal for rkf45.f</span>
    <span class="c"># and therefore removed (class Adaptive adds them in the above statement)</span>
    <span class="n">_optional_parameters</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;first_step&#39;</span><span class="p">)</span>
    <span class="n">_optional_parameters</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;min_step&#39;</span><span class="p">)</span>
    <span class="n">_optional_parameters</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">&#39;max_step&#39;</span><span class="p">)</span>

    <span class="n">_iflag_messages</span> <span class="o">=</span> <span class="p">{</span>
        <span class="mi">2</span><span class="p">:</span>
        <span class="s">&#39;integration reached tout. indicates successful retur &#39;</span>
        <span class="s">&#39;and is the normal mode for continuing integration.&#39;</span><span class="p">,</span>
        <span class="o">-</span><span class="mi">2</span><span class="p">:</span>
        <span class="s">&#39;a single successful step in the direction of tout &#39;</span>
        <span class="s">&#39;has been taken. normal mode for continuing &#39;</span>
        <span class="s">&#39;integration one step at a time.&#39;</span><span class="p">,</span>
        <span class="mi">3</span><span class="p">:</span>
        <span class="s">&#39;integration was not completed because relative error &#39;</span>
        <span class="s">&#39;tolerance was too small. relerr has been increased &#39;</span>
        <span class="s">&#39;appropriately for continuing.&#39;</span><span class="p">,</span>
        <span class="mi">4</span><span class="p">:</span>
        <span class="s">&#39;integration was not completed because more than &#39;</span>
        <span class="s">&#39;3000 derivative evaluations were needed. this &#39;</span>
        <span class="s">&#39;is approximately 500 steps.&#39;</span><span class="p">,</span>
        <span class="mi">5</span><span class="p">:</span>
        <span class="s">&#39;integration was not completed because solution &#39;</span>
        <span class="s">&#39;vanished making a pure relative error test&#39;</span>
        <span class="s">&#39;impossible. must use non-zero abserr to continue. &#39;</span>
        <span class="s">&#39;using the one-step integration mode for one step &#39;</span>
        <span class="s">&#39;is a good way to proceed.&#39;</span><span class="p">,</span>
        <span class="mi">6</span><span class="p">:</span>
        <span class="s">&#39;integration was not completed because requested &#39;</span>
        <span class="s">&#39;accuracy could not be achieved using smallest &#39;</span>
        <span class="s">&#39;allowable stepsize. user must increase the error &#39;</span>
        <span class="s">&#39;tolerance before continued integration can be &#39;</span>
        <span class="s">&#39;attempted.&#39;</span><span class="p">,</span>
        <span class="mi">7</span><span class="p">:</span>
        <span class="s">&#39;it is likely that rkf45 is inefficient for solving &#39;</span>
        <span class="s">&#39;this problem. too much output is restricting the &#39;</span>
        <span class="s">&#39;natural stepsize choice. use the one-step integrator &#39;</span>
        <span class="s">&#39;mode.&#39;</span><span class="p">,</span>
        <span class="mi">8</span><span class="p">:</span>
        <span class="s">&#39;invalid input parameters &#39;</span>
        <span class="s">&#39;this indicator occurs if any of the following is &#39;</span>
        <span class="s">&#39;satisfied - t=tout  and  iflag .ne. +1 or -1 &#39;</span>
        <span class="s">&#39;relerr or abserr .lt. 0. iflag .eq. 0  or  .lt. -2  or  .gt. 8&#39;</span><span class="p">,</span>
        <span class="p">}</span>

<div class="viewcode-block" id="RKF45.initialize"><a class="viewcode-back" href="../../rkf45.html#odespy.rkf45.RKF45.initialize">[docs]</a>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Import extension module _rkf45 and check that it exists.&#39;&#39;&#39;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="kn">import</span> <span class="nn">_rkf45</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_rkf45</span> <span class="o">=</span> <span class="n">_rkf45</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s">&#39;Cannot find the extension module _rkf45.</span><span class="se">\n</span><span class="s">Run setup.py again and investigate why _rkf45.so was not successfully built.&#39;</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="RKF45.adjust_parameters"><a class="viewcode-back" href="../../rkf45.html#odespy.rkf45.RKF45.adjust_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">adjust_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parameters</span><span class="p">[</span><span class="s">&#39;rtol&#39;</span><span class="p">][</span><span class="s">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parameters</span><span class="p">[</span><span class="s">&#39;rtol&#39;</span><span class="p">][</span><span class="s">&#39;extra_check&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">&gt;=</span> <span class="mf">0.0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parameters</span><span class="p">[</span><span class="s">&#39;atol&#39;</span><span class="p">][</span><span class="s">&#39;type&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_parameters</span><span class="p">[</span><span class="s">&#39;atol&#39;</span><span class="p">][</span><span class="s">&#39;extra_check&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">&gt;=</span> <span class="mf">0.0</span>
</div>
<div class="viewcode-block" id="RKF45.initialize_for_solve"><a class="viewcode-back" href="../../rkf45.html#odespy.rkf45.RKF45.initialize_for_solve">[docs]</a>    <span class="k">def</span> <span class="nf">initialize_for_solve</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;f&#39;</span><span class="p">):</span>
            <span class="c"># If f is input in form of f(u,t), wrap f to f_f77 for Fortran code.</span>
            <span class="n">f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">f</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">f_f77</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">t</span><span class="p">,</span><span class="n">u</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">u</span><span class="p">,</span><span class="n">t</span><span class="p">))</span>
        <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;f_f77&#39;</span><span class="p">):</span>
            <span class="c"># If f is input in form of f(t,u) (usually in Fortran),</span>
            <span class="c"># wrap f_f77 to the general form f(u,t) for switch_to()</span>
            <span class="n">f_f77</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">f_f77</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">u</span><span class="p">,</span><span class="n">t</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">f_f77</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">u</span><span class="p">))</span>
        <span class="n">Solver</span><span class="o">.</span><span class="n">initialize_for_solve</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="RKF45.advance"><a class="viewcode-back" href="../../rkf45.html#odespy.rkf45.RKF45.advance">[docs]</a>    <span class="k">def</span> <span class="nf">advance</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">u</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">rtol</span><span class="p">,</span> <span class="n">atol</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">u</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rtol</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">atol</span>

        <span class="n">advance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rkf45</span><span class="o">.</span><span class="n">advance</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">f_f77</span><span class="p">,</span> <span class="s">&#39;_cpointer&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">f_f77</span><span class="p">)</span>
        <span class="n">unew</span><span class="p">,</span> <span class="n">iflag</span> <span class="o">=</span> <span class="n">advance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">u</span><span class="p">[</span><span class="n">n</span><span class="p">],</span> <span class="n">t</span><span class="p">[</span><span class="n">n</span><span class="p">],</span> <span class="n">t</span><span class="p">[</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">],</span> <span class="n">rtol</span><span class="p">,</span> <span class="n">atol</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">iflag</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s">&#39;rkf45.f: iflag=</span><span class="si">%d</span><span class="s"> &gt; 2 (abort)&#39;</span> <span class="o">%</span> <span class="n">iflag</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iflag</span> <span class="o">=</span> <span class="s">&#39;iflag=</span><span class="si">%d</span><span class="se">\n</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">iflag</span><span class="p">,</span> <span class="n">RKF45</span><span class="o">.</span><span class="n">_iflag_messages</span><span class="p">[</span><span class="n">iflag</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">unew</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Odespy API 0.2 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Liwei Wang and Hans Petter Langtangen.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>